package hospital.daoImpl;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import hospital.dao.LoginDao;
import hospital.model.LoginModel;
import hospital.model.UserInfoModel;

public class LoginDaoImpl implements LoginDao {
	private static final Logger log = Logger.getLogger(LoginDaoImpl.class);
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public String selectGetPassword(String username) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(LoginModel.class);
		cr.add(Restrictions.eq("username", username));
		try {
			LoginModel result = (LoginModel) cr.uniqueResult();
			if (result != null) {
				return result.getPassword();
			} else {
				return null;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			log.error("There are duplicate usernames.");
			return null;
		}
	}

	@Override
	public UserInfoModel selectGetUserInfo(String username) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(UserInfoModel.class);
		cr.add(Restrictions.eq("email", username));
		try {
			UserInfoModel result = (UserInfoModel) cr.uniqueResult();
			if (result != null) {
				return result;
			} else {
				return null;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			log.error("There are duplicate usernames.");
			return null;
		}
	}

}
